package com.why.serviceedu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.why.commonresults.Result;
import com.why.serviceedu.bean.Vo.teacherQuery;
import com.why.serviceedu.bean.EduTeacher;
import com.why.serviceedu.service.EduTeacherService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author why
 * @since 2021-01-25
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/edu-teacher")
@CrossOrigin
public class EduTeacherController {

    @Autowired
    private EduTeacherService teacherService;

    @ApiOperation(value = "所有讲师列表")
    @GetMapping("/findAll")
    public Result findAllTeacher() {
        //  int i=1/0;
        List<EduTeacher> list = teacherService.list(null);
        return Result.ok().data("items", list);
    }

    @ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public Result removeById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
        boolean flag = teacherService.removeById(id);

        return flag == true ? Result.ok() : Result.error();
    }

    /**
     * 分页查询
     *
     * @param current：当前页
     * @param limit：每页数量
     * @return
     */

    @GetMapping("/PageTeacher/{current}/{limit}")
    public Result pageTeacher(@PathVariable Long current,
                              @PathVariable Long limit) {
        //参数一个是分页的对象，一个是查询条件
        //会像pagerHelper一样，返回所有关于分页的信息
        Page<EduTeacher> page = new Page<>(current, limit);
        teacherService.page(page, null);
        //总记录数
        long total = page.getTotal();
        //数据list集合
        List<EduTeacher> records = page.getRecords();
        //要么使用链式编程，要么传递一个map
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        return Result.ok().data(map);
    }


    /**
     * 注解@RequestBody 将json对象封装成一个java对象，适用于post
     *
     * @param current
     * @param limit
     * @param query
     * @return
     */
    @PostMapping("/teacherQeuryCondition/{current}/{limit}")
    public Result teacherQeuryCondition(@PathVariable Long current,
                                        @PathVariable Long limit,
                                        @RequestBody(required = false) teacherQuery query) {
        //参数一个是分页的对象，一个是查询条件
        //会像pagerHelper一样，返回所有关于分页的信息
        Page<EduTeacher> page = new Page<>(current, limit);
        //new 一个wrapper，进行条件查询
        QueryWrapper<EduTeacher> wrapper = new QueryWrapper<>();
        String name = query.getName();
        Integer level = query.getLevel();
        String begin = query.getBegin();
        String end = query.getEnd();
        if (!StringUtils.isEmpty(name)) {
            //模糊查询
            wrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            //模糊查询
            wrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            //模糊查询
            wrapper.ge("gmt_create", begin);
        }

        if (!StringUtils.isEmpty(end)) {
            //模糊查询
            wrapper.le("gmt_create", end);
        }


        //进行动态sql的判断与添加

        teacherService.page(page, wrapper);
        //总记录数
        long total = page.getTotal();
        //数据list集合
        List<EduTeacher> records = page.getRecords();
        //要么使用链式编程，要么传递一个map
        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("rows", records);
        return Result.ok().data(map);
    }

    @ApiOperation(value = "新增讲师")
    @PostMapping("addTeacher")
    public Result save(
            @ApiParam(name = "teacher", value = "讲师对象", required = true)
            @RequestBody EduTeacher teacher) {
        boolean flag = teacherService.save(teacher);
        return flag ? Result.ok() : Result.error();
    }

    /**
     * 根据id查询老师信息
     * @param id
     * @return
     */

    @GetMapping("/getTeacher/{id}")
    public  Result getTeacher(@PathVariable Long id){
        EduTeacher teacher = teacherService.getById(id);
        return Result.ok().data("idTeacher",teacher);

    }


    /**
     * 修改方法
     * @param teacher
     * @return
     */
    @PostMapping("/updateTeacher")
    public  Result updateTeacher(@RequestBody EduTeacher teacher){

        //传过来的必须有id
        boolean flag = teacherService.updateById(teacher);
        return flag ? Result.ok() : Result.error();

    }




}

